package com.gxa.controller.employee;

import com.gxa.common.R.ResponseData;
import com.gxa.service.employee.PhotoUploadService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;

/**
 * 图片上传controller层
 */
@Controller
@Api(value = "图片上传test",tags = "图片上传接口API")
@RequestMapping("/upload")
public class PhotoUploadController {
    @Autowired(required = false)
    private PhotoUploadService photoUploadService;

    //人像照片上传，后台返回图片存储路径
    @RequestMapping(value = "/peoplePhotoUpload",method = RequestMethod.POST)   //get没有上传
    @ApiOperation(value = "人像照片上传接口",notes = "人像照片上传，后台返回图片存储路径",httpMethod = "post", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResponseData peoplePhotoUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        ResponseData r = new ResponseData();
        HashMap<Object, Object> map = new HashMap<>();
        if(file.isEmpty()) {
            map.put("code", 500);
            map.put("msg", "上传失败!!!");
        }else {
            String fileUrl = photoUploadService.photoUpload(file);
            map.put("peoplePhoto",fileUrl);
        }
        r.setCode(200);
        r.setMsg("人像照片上传成功");
        r.setData(map);
        return r;
    }

    //入职表照片上传，返回入职表存储路径
    @RequestMapping(value = "/entryFormPhotoUpload",method = RequestMethod.POST)
    @ApiOperation(value = "入职表照片上传接口",notes = "入职表照片上传，返回入职表存储路径",httpMethod = "post", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResponseData entryFormPhotoUpload(MultipartFile file, HttpServletRequest request) throws IOException {
        ResponseData r = new ResponseData();
        HashMap<Object, Object> map = new HashMap<>();
        if(file.isEmpty()) {
            map.put("code", 500);
            map.put("msg", "上传失败!!!");
        }else {
            String fileUrl = photoUploadService.photoUpload(file);
            map.put("entryFormPhoto",fileUrl);
        }
        r.setCode(200);
        r.setMsg("入职表照片上传成功");
        r.setData(map);
        return r;
//        map.put("entryFormPhoto","Uploads/Perfect/fujian/20220329/6242a0a13efd1.JPG ");
    }

    //身份证附件上传，返回身份证附件存储路径
    @RequestMapping(value = "/identityCardPhotoUpload",method = RequestMethod.POST)
    @ApiOperation(value = "身份证附件上传接口",notes = "身份证附件上传，返回身份证附件存储路径",httpMethod = "post", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResponseData identityCardPhotoUpload(MultipartFile file, HttpServletRequest request) throws IOException {
        ResponseData r = new ResponseData();
        HashMap<Object, Object> map = new HashMap<>();
        if(file.isEmpty()) {
            map.put("code", 500);
            map.put("msg", "上传失败!!!");
        }else {
            String fileUrl = photoUploadService.photoUpload(file);
            map.put("identityCardPhoto",fileUrl);
        }
        r.setCode(200);
        r.setMsg("身份证附件上传成功");
        r.setData(map);
        return r;
        //        map.put("identityCardPhoto","Uploads/Perfect/shenfenzheng/20220329/6242a0bd4e285.JPG  ");
    }

    //离职表附件上传，返回离职表附件存储路径
    @RequestMapping(value = "/quitFormPhotoUpload",method = RequestMethod.POST)
    @ApiOperation(value = "离职表附件上传接口",notes = "离职表附件上传，返回离职表附件存储路径",httpMethod = "post", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResponseData quitFormPhotoUpload(MultipartFile file, HttpServletRequest request) throws IOException {
        ResponseData r = new ResponseData();
        HashMap<Object, Object> map = new HashMap<>();
        if(file.isEmpty()) {
            map.put("code", 500);
            map.put("msg", "上传失败!!!");
        }else {
            String fileUrl = photoUploadService.photoUpload(file);
            map.put("quitFormPhoto",fileUrl);
        }
        r.setCode(200);
        r.setMsg("离职表附件上传成功");
        r.setData(map);
        return r;
        //        map.put("quitFormPhoto","Uploads/Staff/lizhibiao/20220401/6246e7697fae2.jpg ");
    }
//        {
//            "code": 200,
//                "msg": "人像照片上传成功",
//                "data": {
//            "peoplePhoto": "http://ra2kk7kmp.hn-bkt.clouddn.com/B54jeU.jpg"
//        },
//            "token": null,
//                "total": null
//        }
    /**
     * 获取系统当前时间转化为20220404格式
     * @return          返回一个String类型的年月日
     */
//    public static String getNowTime() {
//        Calendar calendar = Calendar.getInstance();
//        int Y = calendar.get(Calendar.YEAR);         //获取系统当前时间年
//        int m = calendar.get(Calendar.MONTH)+1;      //获取系统当前时间月+1：因为月份从0开始所以要+1
//        String M = null;
//        if (m < 10){
//            M = "0"+m;
//        }
//        int d = calendar.get(Calendar.DATE);         ////获取系统当前时间日
//        String D = null;
//        if (d < 10){
//            D = "0"+d;
//        }
//        String nowTime = Y+M+D;
//        return nowTime;
//    }
}
